// created on 21.07.2007 at 13:52
using Gtk;
using System;
using System.Text.RegularExpressions;
public class NumEntry : Entry
{
	public NumEntry()
	{
		//this.KeyPressEvent +=
		this.TextInserted += OnInserted;
		this.TextDeleted += OnDeleted;
		//this.Text = "0";
		Num = 0;
		
	}
	bool IsNumeric(string text)
	{
	    return Regex.IsMatch(text,regex);
	}
	
	string regex = "^-?\\d*$";
	int num;
	int minval;
	bool usemin = false;
	
	public int MinValue
	{
		set
		{
			if(value < 10 && value > 0)
			{
			minval = value;
			usemin = true;
			}
			else if(value == 0)
			{
			regex = "^\\d*$";
			}
		}
	}
	public int Num
	{
		get
		{
			return num;
		}
		set
		{
			if(IsNumeric(value.ToString()))
			{
				num = value;
				Text = value.ToString();
			}
		}
	}
	
	protected void OnInserted(object o, Gtk.TextInsertedArgs args)
	{
		if(!IsNumeric(Text))
		{
			Text = num.ToString();
		}
		else
			if(Text == "-")
			{
				if(!usemin || (usemin && minval <= 0))
					num = 0;
				else
					Text = num.ToString();
			}
			else
			{
				if(!usemin || (usemin && int.Parse(Text) >= minval))
					num = int.Parse(Text);
				else
					Text = num.ToString();
			}
	}
	protected void OnDeleted(object o, TextDeletedArgs args)
	{
		try
		{
		int i = int.Parse(Text);
		if(!usemin || (usemin && i >= minval))
			num = i;
		else
			Text = num.ToString();
		}
		catch(Exception){}
		
	}
	
}